package com.serkol.ams.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Category;
import com.serkol.ams.modal.SqlFeildModal;
import com.serkol.ams.service.CategoryService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/category")
public class CategoryApiController {
    @Value("${upload.host}")
    private String uploadhost;
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/findByList")
    public Result  findByList(){
      QueryWrapper<Category> query = new QueryWrapper<>();
        query.select(SqlFeildModal.getCategoryFeild());
        query.lambda().orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(query);
        return Result.ok(list);
    }
    @IsLogin
    @GetMapping("/findBySelectList")
    public Result  findBySelectList(){
        QueryWrapper<Category> query = new QueryWrapper<>();
        query.select(SqlFeildModal.getCategorySelectFeild());
        query.lambda().orderByAsc(Category::getSort);
        List<Map<String, Object>> list = categoryService.listMaps(query);

        return Result.ok(list);
    }
    @IsLogin
    @GetMapping("/findByPageList")
    public Result findByPageList(@RequestParam(name="pageSize", defaultValue = "10") int pageSize, @RequestParam(name="page", defaultValue = "0") int current){
        AuthConstant.setPathUrl(uploadhost);
        Result result = new Result();
      return categoryService.findByList(pageSize, current);

    }
    @IsLogin
    @RequestMapping("edit")
    public Result edit(@RequestBody Category sysCategory) {

        boolean b = false;
        if (sysCategory.getId() != null) {
            b = categoryService.updateById(sysCategory);
        } else {
            b = categoryService.save(sysCategory);
        }
        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);
    }

}
